cmake_minimum_required(VERSION 2.8)

if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
  if(DEFINED ENV{VITASDK})
    set(CMAKE_TOOLCHAIN_FILE "$ENV{VITASDK}/share/vita.toolchain.cmake" CACHE PATH "toolchain file")
  else()
    message(FATAL_ERROR "Please define VITASDK to point to your SDK path!")
  endif()
endif()

project(adrenaline_kernel)
include("${VITASDK}/share/vita.cmake" REQUIRED)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-q -Wall -O3 -nostdlib")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fno-exceptions")

# Builds
FUNCTION(ADD_RESOURCES out_var)
  SET(result)
  FOREACH(in_f ${ARGN})
  SET(out_f "${CMAKE_CURRENT_BINARY_DIR}/${in_f}.o")
  GET_FILENAME_COMPONENT(out_dir ${out_f} DIRECTORY)
  ADD_CUSTOM_COMMAND(OUTPUT ${out_f}
    COMMAND ${CMAKE_COMMAND} -E make_directory ${out_dir}
    COMMAND cd ${CMAKE_BINARY_DIR} && ${CMAKE_LINKER} -r -b binary -o ${out_f} ${in_f}
    DEPENDS ${CMAKE_BINARY_DIR}/${in_f}
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    COMMENT "Building resource ${out_f}"
    VERBATIM
    )
  LIST(APPEND result ${out_f})
  ENDFOREACH()
  SET(${out_var} "${result}" PARENT_SCOPE)
ENDFUNCTION()

add_resources(res_files
  modules/vsh/adrenaline_vsh.suprx
)

add_executable(adrenaline_kernel
  ${res_files}
  main.c
  utils.c
)

target_link_libraries(adrenaline_kernel
  taihenForKernel_stub
  taihenModuleUtils_stub
  SceCpuForDriver_stub
  SceCtrlForDriver_stub
  SceIofilemgrForDriver_stub
  SceModulemgrForDriver_stub
  SceProcessmgrForDriver_stub
  SceSysclibForDriver_stub
  SceSysmemForDriver_stub
  SceSysrootForKernel_stub
  SceSysrootForDriver_stub
  SceThreadmgrForDriver_stub
  SceDebugForDriver_stub
)

vita_create_self(adrenaline_kernel.skprx adrenaline_kernel CONFIG exports.yml UNSAFE)

vita_create_stubs(adrenaline_kernel_stubs adrenaline_kernel exports.yml KERNEL)
